<?php
namespace app\index\controller;

use app\index\model\ContractCaiModel;
use app\index\model\DeviceJieListModel;
use app\index\model\DeviceJieModel;
use app\index\model\DeviceJinListModel;
use app\index\model\DeviceJinModel;
use app\index\model\DeviceLinListModel;
use app\index\model\DeviceLinModel;
use app\index\model\DeviceTuiModel;
use app\index\model\DeviceYunModel;
use app\index\model\EnginModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\service\DeviceService;
use app\index\service\EnginService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Device extends Common
{
    public function list_device_jin(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jin.deleted_user','=', 0];
        $where[] = ['jin.comid','=',$this->userinfo['comid']];
        $where[] = ['jin.isturn','=',1];
        $where[] = ['jin.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|cai.name|cai.gonger|jin.number|jin.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['jin.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['caigonger']) && !empty($param['caigonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['caigonger'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jin.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['jin.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['jin.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jin.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = DeviceJinModel::alias('jin')
            ->field('jin.*,e.name as enginname,cai.name as cainame,cai.number as cainumber,cai.gonger as caigonger')
            ->join('engin e','e.id=jin.enginid')
            ->join('contract_cai cai','cai.id=jin.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('device_jin', 'cai'))
            ->order('jin.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){


                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('device_jin');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_device_jin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择工程名称');
            }
            if(empty($param['caiid'])){
                return outputJson(4000, '请选择合同名称');
            }


            $param['dt'] = date("Y-m-d");
            $param['baner'] = $this->userinfo['realname'];
            $param['number'] = (new DeviceService())->create_device_jin_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入进场明细');
            }

            Db::startTrans();
            try {
                $id = DeviceJinModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jinid'] = $id;
                    DeviceJinListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'device_jin',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'device_jin', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'device_jin', $id, '进场登记');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new DeviceService())->create_device_jin_number();
            $data['files'] = (new OptService())->get_file('device_jin');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_device_jin(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = DeviceJinModel::where(['id'=>$param['id']])->find();


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入进场明细');
            }

            Db::startTrans();
            try {
                DeviceJinModel::update($param);

                DeviceJinListModel::where(['jinid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jinid'] = $param['id'];
                    DeviceJinListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'device_jin',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'device_jin', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'device_jin', $param['id'], '进场登记');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DeviceJinModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $caiidinfo = ContractCaiModel::get_info_by_pk($data['data']['caiid']);
            $data['caiidinfo'] = $caiidinfo;

            $list = DeviceJinListModel::where(['jinid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('device_jin', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_device_jin(Request $request){
        $param = $request->post();
        $data = DeviceJinModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            DeviceJinModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'device_jin', $data['id'], '进场登记');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_device_jin(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = DeviceJinModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['caiidinfo'] = ContractCaiModel::get_info_by_pk($data['data']['caiid']);


        $list = DeviceJinListModel::where(['jinid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('device_jin', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }




    public function list_device_mingxi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jin.deleted_user','=', 0];
        $where[] = ['jin.comid','=',$this->userinfo['comid']];
        $where[] = ['jin.isturn','=',1];
        $where[] = ['jin.status','=', 1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|cai.name|cai.gonger|jin.number|list.name|list.shenum','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['jin.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['caigonger']) && !empty($param['caigonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['caigonger'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jin.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['jin.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['jin.baner','like', '%'.$param['baner'].'%'];
        }

        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['listunit']) && !empty($param['listunit'])){
            $where[] = ['list.unit','like', '%'.$param['listunit'].'%'];
        }
        if(isset($param['listprice']) && !empty($param['listprice'])){
            $where[] = ['list.price','like', '%'.$param['listprice'].'%'];
        }
        if(isset($param['listdigit']) && !empty($param['listdigit'])){
            $where[] = ['list.digit','like', '%'.$param['listdigit'].'%'];
        }
        if(isset($param['listjindt']) && !empty($param['listjindt'])){
            $where[] = ['list.jindt','between', explode(' - ', $param['listjindt'])];
        }
        if(isset($param['listshenum']) && !empty($param['listshenum'])){
            $where[] = ['list.shenum','like', '%'.$param['listshenum'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = DeviceJinListModel::alias('list')
            ->field('jin.*,e.name as enginname,cai.name as cainame,cai.gonger,list.id as listid,list.name as listname,list.unit as listunit
            ,list.price as listprice,list.digit as listdigit,list.jindt as listjindt,list.shenum as listshenum')
            ->join('device_jin jin','jin.id=list.jinid')
            ->join('engin e','e.id=jin.enginid')
            ->join('contract_cai cai','cai.id=jin.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('device_mingxi', 'cai'))
            ->order('jin.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['status_name'] = '在场';
                $item['tuidt'] = '';
                $tui = DeviceTuiModel::where(['enginid'=>$item['enginid'],'caiid'=>$item['caiid'],'deleted_user'=>0,'jinid'=>$item['listid']])->find();
                if($tui){
                    $item['status_name'] = '退场';
                    $item['tuidt'] = $tui['tuidt'];
                    $item['days'] = count_days($item['listjindt'], $tui['tuidt']);
                }else{
                    $item['days'] = count_days($item['listjindt'], date("Y-m-d"));
                }

                $item['yununit'] = DeviceYunModel::where(['enginid'=>$item['enginid'],'caiid'=>$item['caiid'],'deleted_user'=>0,'jinid'=>$item['listid']])->sum('yununit');

                $item['leijieunit'] =  DeviceJieListModel::alias('list')
                    ->join('device_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1])
                    ->where('jie.status','in',[0,1])
                    ->where('list.listid','=',$item['listid'])
                    ->sum('jieunit');
                $item['leijiemoney'] =  DeviceJieListModel::alias('list')
                    ->join('device_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1])
                    ->where('jie.status','in',[0,1])
                    ->where('list.listid','=',$item['listid'])
                    ->sum('list.money');
                $item['daijieunit'] = $item['yununit'] - $item['leijieunit'];


                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('device_mingxi');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function detail_device_mingxi(Request $request){
        $param = $request->post();

        $data = [];

        $listinfo = DeviceJinListModel::where(['id'=>$param['jinid']])->find();
        $jininfo = DeviceJinModel::where(['id'=>$listinfo['jinid']])->find();

        $enginname = EnginModel::where(['id'=>$jininfo['enginid']])->value('name');
        $caiid_name = ContractCaiModel::where(['id'=>$jininfo['caiid']])->value('name');
        $gonger = ContractCaiModel::where(['id'=>$jininfo['caiid']])->value('gonger');

        $init = [
            'type' => 'jin',
            'id' => $listinfo['id'],
            'number' => $jininfo['number'],
            'dt' => $jininfo['dt'],
            'baner' => $jininfo['baner'],
            'dongname' => '进场',
            'name' => $listinfo['name'],
            'shenum' => $listinfo['shenum'],
            'unit' => $listinfo['unit'],
            'price' => $listinfo['price'],
            'jindt' => $listinfo['jindt'],
            'tuidt' => '',
            'dts' => '',
            'yunhour' => '',
            'yununit' => '',
            'gonger' => $gonger,
            'enginname' => $enginname,
            'caiid_name' => $caiid_name,
        ];
        $data[] = $init;

        $other = [];
        $yun = DeviceYunModel::where(['enginid'=>$jininfo['enginid'],'caiid'=>$jininfo['caiid'],'jinid'=>$listinfo['id']])->select()->toArray();
        foreach ($yun as $v){
            $init_chu = $init;
            $init_chu['type'] = 'yun';
            $init_chu['id'] = $v['id'];
            $init_chu['number'] = $v['number'];
            $init_chu['dt'] = $v['dt'];
            $init_chu['baner'] = $v['baner'];
            $init_chu['dongname'] = '运行';
            $init_chu['jindt'] = '';
            $init_chu['dts'] = $v['dts'];
            $init_chu['yunhour'] = $v['yunhour'];
            $init_chu['yununit'] = $v['yununit'];
            $other[] = $init_chu;
        }

        $tui = DeviceTuiModel::where(['enginid'=>$jininfo['enginid'],'caiid'=>$jininfo['caiid'],'jinid'=>$listinfo['id']])->select()->toArray();
        foreach ($tui as $v){
            $init_chu = $init;
            $init_chu['type'] = 'tui';
            $init_chu['id'] = $v['id'];
            $init_chu['number'] = $v['number'];
            $init_chu['dt'] = $v['dt'];
            $init_chu['baner'] = $v['baner'];
            $init_chu['dongname'] = '退场';
            $init_chu['jindt'] = '';
            $init_chu['tuidt'] = $v['tuidt'];
            $other[] = $init_chu;
        }


        $dtSort = array_column($other,'dt');
        array_multisort($dtSort,SORT_ASC,$other);

        foreach ($other as $vvv){
            $data[] = $vvv;
        }


        return outputJson(2000,lang('code_2000'),$data);
    }
    public function yun_device_mingxi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择工程名称');
            }
            if(empty($param['caiid'])){
                return outputJson(4000, '请选择合同名称');
            }



            $param['dt'] = date("Y-m-d");
            $param['baner'] = $this->userinfo['realname'];
            $param['comid'] = $this->userinfo['comid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['deptid'] = $this->userinfo['deptid'];
            $param['number'] = (new DeviceService())->create_device_yun_number();
            $param['created_user'] = $this->userinfo['id'];
            $param['created_at'] = date('Y-m-d H:i:s');



            $list = array_filter($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入运行明细');
            }


            foreach ($list as &$item){
                $item['comid'] = $param['comid'];
                $item['chanid'] = $param['chanid'];
                $item['deptid'] = $param['deptid'];
                $item['enginid'] = $param['enginid'];
                $item['caiid'] = $param['caiid'];
                $item['number'] = $param['number'];
                $item['dt'] = $param['dt'];
                $item['baner'] = $param['baner'];
                $item['created_user'] = $param['created_user'];
                $item['created_at'] = $param['created_at'];
            }


            Db::startTrans();
            try {
                foreach($list as $v){
                    if(is_array($v['dts'])){
                        $v['dts'] = implode(' - ',$v['dts']);
                    }
                    DeviceYunModel::insert($v);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $jinid = $request->param('jinid', 0, 'int');

            $jinlistinfo = DeviceJinListModel::where(['id'=>$jinid])->find();
            if($jinlistinfo){
                $jininfo = DeviceJinModel::where(['id'=>$jinlistinfo['jinid']])->find();
                $data['engininfo'] = EnginModel::get_info_by_pk($jininfo['enginid']);
                $data['caiidinfo'] = ContractCaiModel::get_info_by_pk($jininfo['caiid']);

                $data['list'] = [
                    'jinid' => $jinlistinfo['id'],
                    'name' => $jinlistinfo['name'],
                    'shenum' => $jinlistinfo['shenum'],
                    'unit' => $jinlistinfo['unit'],
                    'price' => $jinlistinfo['price'],
                    'jindt' => $jinlistinfo['jindt'],
                ];
            }
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new DeviceService())->create_device_yun_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function yun_edit_device_mingxi(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $list = array_filter($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入运行明细');
            }

            Db::startTrans();
            try {
                foreach($list as $v){
                    if(is_array($v['dts'])){
                        $v['dts'] = implode(' - ',$v['dts']);
                    }
                    DeviceYunModel::where(['id'=>$v['id']])->update([
                        'dts' => $v['dts'],
                        'yunhour' => $v['yunhour'],
                        'yununit' => $v['yununit'],
                        'explain' => $v['explain'],
                    ]);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->param('id', 0, 'int');

            $data['data'] = DeviceYunModel::where(['id'=>$id])->find();

            $jinlistinfo = DeviceJinListModel::where(['id'=>$data['data']['jinid']])->find();
            if($jinlistinfo){
                $jininfo = DeviceJinModel::where(['id'=>$jinlistinfo['jinid']])->find();
                $data['engininfo'] = EnginModel::get_info_by_pk($jininfo['enginid']);
                $data['caiidinfo'] = ContractCaiModel::get_info_by_pk($jininfo['caiid']);

                $data['list'] = [
                    'id' => $data['data']['id'],
                    'jinid' => $jinlistinfo['id'],
                    'name' => $jinlistinfo['name'],
                    'shenum' => $jinlistinfo['shenum'],
                    'unit' => $jinlistinfo['unit'],
                    'price' => $jinlistinfo['price'],
                    'jindt' => $jinlistinfo['jindt'],
                    'dts' => $data['data']['dts'],
                    'dtsarr' => explode(' - ', $data['data']['dts']),
                    'yunhour' => $data['data']['yunhour'],
                    'yununit' => $data['data']['yununit'],
                    'explain' => $data['data']['explain'],
                ];
            }
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function tui_device_mingxi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择工程名称');
            }
            if(empty($param['caiid'])){
                return outputJson(4000, '请选择合同名称');
            }



            $param['dt'] = date("Y-m-d");
            $param['baner'] = $this->userinfo['realname'];
            $param['comid'] = $this->userinfo['comid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['deptid'] = $this->userinfo['deptid'];
            $param['number'] = (new DeviceService())->create_device_tui_number();
            $param['created_user'] = $this->userinfo['id'];
            $param['created_at'] = date('Y-m-d H:i:s');



            $list = array_filter($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入退场明细');
            }


            foreach ($list as &$item){
                $item['comid'] = $param['comid'];
                $item['chanid'] = $param['chanid'];
                $item['deptid'] = $param['deptid'];
                $item['enginid'] = $param['enginid'];
                $item['caiid'] = $param['caiid'];
                $item['number'] = $param['number'];
                $item['dt'] = $param['dt'];
                $item['baner'] = $param['baner'];
                $item['created_user'] = $param['created_user'];
                $item['created_at'] = $param['created_at'];
            }


            Db::startTrans();
            try {
                foreach($list as $v){
                    DeviceTuiModel::insert($v);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $jinid = $request->param('jinid', 0, 'int');

            $jinlistinfo = DeviceJinListModel::where(['id'=>$jinid])->find();
            if($jinlistinfo){
                $jininfo = DeviceJinModel::where(['id'=>$jinlistinfo['jinid']])->find();
                $data['engininfo'] = EnginModel::get_info_by_pk($jininfo['enginid']);
                $data['caiidinfo'] = ContractCaiModel::get_info_by_pk($jininfo['caiid']);

                $yununit = DeviceYunModel::where(['enginid'=>$jininfo['enginid'],'caiid'=>$jininfo['caiid'],'deleted_user'=>0,'jinid'=>$jinlistinfo['id']])->sum('yununit');

                $data['list'] = [
                    'jinid' => $jinlistinfo['id'],
                    'name' => $jinlistinfo['name'],
                    'shenum' => $jinlistinfo['shenum'],
                    'digit' => $jinlistinfo['digit'],
                    'jindt' => $jinlistinfo['jindt'],
                    'yununit' => $yununit,
                ];
            }
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new DeviceService())->create_device_tui_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function tui_edit_device_mingxi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入退场明细');
            }

            Db::startTrans();
            try {
                foreach($list as $v){
                    DeviceTuiModel::where(['id'=>$v['id']])->update([
                        'tuidt' => $v['tuidt'],
                        'explain' => $v['explain'],
                    ]);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->param('id', 0, 'int');

            $data['data'] = DeviceTuiModel::where(['id'=>$id])->find();
            $jinlistinfo = DeviceJinListModel::where(['id'=>$data['data']['jinid']])->find();
            if($jinlistinfo){
                $jininfo = DeviceJinModel::where(['id'=>$jinlistinfo['jinid']])->find();
                $data['engininfo'] = EnginModel::get_info_by_pk($jininfo['enginid']);
                $data['caiidinfo'] = ContractCaiModel::get_info_by_pk($jininfo['caiid']);

                $yununit = DeviceYunModel::where(['enginid'=>$jininfo['enginid'],'caiid'=>$jininfo['caiid'],'deleted_user'=>0,'jinid'=>$jinlistinfo['id']])->sum('yununit');

                $data['list'] = [
                    'id' => $data['data']['id'],
                    'jinid' => $jinlistinfo['id'],
                    'name' => $jinlistinfo['name'],
                    'shenum' => $jinlistinfo['shenum'],
                    'digit' => $jinlistinfo['digit'],
                    'jindt' => $jinlistinfo['jindt'],
                    'yununit' => $yununit,
                    'tuidt' => $data['data']['tuidt'],
                    'explain' => $data['data']['explain'],
                ];
            }

            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function del_device_yun(Request $request){
        $param = $request->post();
        $data = DeviceYunModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            DeviceYunModel::where(['id'=>$data['id']])->delete();
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function del_device_tui(Request $request){
        $param = $request->post();
        $data = DeviceTuiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            DeviceTuiModel::where(['id'=>$data['id']])->delete();
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }


    public function list_device_jie(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|cai.name|cai.gonger|jie.number|jie.money|jie.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['jie.enginid','=', $param['enginid']];
        }
        if(isset($param['caiid']) && !empty($param['caiid'])){
            $where[] = ['jie.caiid','=', $param['caiid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['caigonger']) && !empty($param['caigonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['caigonger'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jie.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['jie.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['jie.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['jie.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jie.status','=', $param['status']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = DeviceJieModel::alias('jie')
            ->field('jie.*,e.name as enginname,cai.name as cainame,cai.number as cainumber,cai.gonger as caigonger')
            ->join('engin e','e.id=jie.enginid')
            ->join('contract_cai cai','cai.id=jie.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('device_jie', 'cai'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){


                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('device_jie');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_device_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择工程名称');
            }
            if(empty($param['caiid'])){
                return outputJson(4000, '请选择合同名称');
            }


            $param['dt'] = date("Y-m-d");
            $param['baner'] = $this->userinfo['realname'];
            $param['number'] = (new DeviceService())->create_device_jie_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入结算明细');
            }

            Db::startTrans();
            try {
                $id = DeviceJieModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jieid'] = $id;
                    DeviceJieListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'device_jie',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'device_jie', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'device_jie', $id, '租赁结算');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new DeviceService())->create_device_jie_number();
            $data['files'] = (new OptService())->get_file('device_jie');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_device_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = DeviceJieModel::where(['id'=>$param['id']])->find();


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入结算明细');
            }

            Db::startTrans();
            try {
                DeviceJieModel::update($param);

                DeviceJieListModel::where(['jieid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jieid'] = $param['id'];
                    DeviceJieListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'device_jie',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'device_jie', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'device_jie', $param['id'], '租赁结算');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DeviceJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $caiidinfo = ContractCaiModel::get_info_by_pk($data['data']['caiid']);
            $data['caiidinfo'] = $caiidinfo;

            $list = DeviceJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('device_jie', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_device_jie(Request $request){
        $param = $request->post();
        $data = DeviceJieModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            DeviceJieModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'device_jie', $data['id'], '租赁结算');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_device_jie(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = DeviceJieModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['caiidinfo'] = ContractCaiModel::get_info_by_pk($data['data']['caiid']);


        $list = DeviceJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('device_jie', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_device_lin(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['lin.deleted_user','=', 0];
        $where[] = ['lin.comid','=',$this->userinfo['comid']];
        $where[] = ['lin.isturn','=',1];
        $where[] = ['lin.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|lin.gonger|lin.number|lin.money|lin.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['lin.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['lin.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['lin.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['lin.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['lin.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['lin.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['lin.status','=', $param['status']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = DeviceLinModel::alias('lin')
            ->field('lin.*,e.name as enginname')
            ->join('engin e','e.id=lin.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('device_lin', 'lin'))
            ->order('lin.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('device_lin');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_device_lin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择工程名称');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请选择销售方');
            }


            $param['dt'] = date("Y-m-d");
            $param['baner'] = $this->userinfo['realname'];
            $param['number'] = (new DeviceService())->create_device_lin_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入登记明细');
            }

            Db::startTrans();
            try {
                $id = DeviceLinModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['linid'] = $id;
                    DeviceLinListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'device_lin',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'device_lin', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'device_lin', $id, '临时机械登记');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new DeviceService())->create_device_lin_number();
            $data['files'] = (new OptService())->get_file('device_lin');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_device_lin(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = DeviceLinModel::where(['id'=>$param['id']])->find();


            if(empty($param['gonger'])){
                return outputJson(4000, '请选择销售方');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入登记明细');
            }

            Db::startTrans();
            try {
                DeviceLinModel::update($param);

                DeviceLinListModel::where(['linid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['linid'] = $param['id'];
                    DeviceLinListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'device_lin',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'device_lin', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'device_lin', $param['id'], '临时机械登记');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DeviceLinModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $list = DeviceLinListModel::where(['linid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('device_lin', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_device_lin(Request $request){
        $param = $request->post();
        $data = DeviceLinModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            DeviceLinModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'device_lin', $data['id'], '临时机械登记');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_device_lin(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = DeviceLinModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);


        $list = DeviceLinListModel::where(['linid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('device_lin', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


}
